﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace SCG.TurboSprite
{
    public class ExplosionParticle
    {
        //constructor
        public ExplosionParticle(ParticleExplosionSprite explosion)
        {
            Color = Sprite.ColorFromRange(explosion.StartColor, explosion.EndColor);
            DirectionX = Sprite.RND.NextDouble() * 8 - 4;
            DirectionY = Sprite.RND.NextDouble() * 8 - 4;
            Diameter = Sprite.RND.Next(explosion.EndDiameter - explosion.StartDiameter) + explosion.StartDiameter;
            Alpha = 255;
            AlphaDec = Sprite.RND.Next(30);
            Parent = explosion;
        }

        //properties
        public Sprite Parent { get; set; }
        public double X { get; set; }
        public double Y { get; set; }
        public Color Color { get; set; }
        public double DirectionX { get; set; }
        public double DirectionY { get; set; }
        public int Diameter { get; set; }
        public byte Alpha { get; set; }
        public int AlphaDec { get; set; }
        public double Gravity { get; set; }

        //processing
        public virtual void Process()
        {
            int newAlpha = Alpha - AlphaDec;
            if (newAlpha < 0)
                newAlpha = 0;
            Alpha = (byte)newAlpha;
            X += DirectionX;
            Y += DirectionY;
            Gravity += Sprite.RND.NextDouble() * 0.5;
            Y += Gravity;
        }

        //rendering
        public virtual void Render(Graphics g, SpriteSurface surface)
        {
            if (surface == null)
                return;
            int x = (int)(Parent.X - surface.OffsetX + X - Diameter / 2);
            int y = (int)(Parent.Y - surface.OffsetY + Y - Diameter / 2);
            SolidBrush b = new SolidBrush(Color.FromArgb(Alpha, Color));
            g.FillEllipse(b, x, y, Diameter * 2, Diameter * 2);
        }
    }
}
